package com.zhuqi.message.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * <p>类描述</p>
 * Copyright: Copyright (C) 2023 YinTa, Inc. All rights reserved.
 * @author zhuqi
 * @since 2023/4/13 9:43
 */
@Component
@Slf4j
public class Subscriber implements MessageListener {

	@Autowired
	@Qualifier(value = "pubSubStrRedisTemplate")
	private RedisTemplate<String,String> redisTemplate;
	@Override
	public void onMessage(Message message, byte[] pattern) {
		RedisSerializer<?> keySerializer = redisTemplate.getKeySerializer();
		RedisSerializer<?> valueSerializer = redisTemplate.getValueSerializer();
		String key = (String) keySerializer.deserialize(message.getChannel());
		String value = (String) valueSerializer.deserialize(message.getBody());
		log.info("消息 key:{}, value:{}", key, value);
	}
}
